﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Core.IService;
using FX.Core;
using BHXH.Core.Domain;
using Infragistics.Win.UltraWinGrid;
using CRM.Admin;
using NHibernate;

namespace BHXH.Frm.Frm
{
    public partial class DKBHXH : UserControl
    {
        private readonly IhosoService _hosoSrv;
        private readonly IthanhphoService _thanhphoSrv;
        private readonly IbenhvienService _benhvienSrv;
        private readonly Ihs_A01aTS_dtService _hs_01adtSrv;

        UltraCombo uc_Quoctich;
        UltraCombo uc_TinhThanhPho;
        UltraCombo uc_Loai;
        UltraCombo uc_Dantoc;
        UltraCombo uc_Benhvien;

        IList<string> ID_A01aTS = new List<string>();

        public DKBHXH()
        {
            InitializeComponent();
            _hosoSrv = IoC.Resolve<IhosoService>();
            _thanhphoSrv = IoC.Resolve<IthanhphoService>();
            _benhvienSrv = IoC.Resolve<IbenhvienService>();
            _hs_01adtSrv = IoC.Resolve<Ihs_A01aTS_dtService>();


            LoadDataGridview();
            
            
            LoadComboBox();
            uG_A01aTS.DataSource = GetAll_A01aTS_Tab();
            uG_A01aTS.DisplayLayout.Override.ActiveCellAppearance.Reset();
            uG_A01aTS.DisplayLayout.Override.ActiveRowAppearance.Reset();
            LoadGridview_A01aTS();

        }

        #region UG_DanhMucHoSo
        public void LoadDataGridview()
        {
            uG_DanhMucHS.DataSource = GetAllHoSo();
            uG_DanhMucHS.DisplayLayout.Override.ActiveCellAppearance.Reset();
            uG_DanhMucHS.DisplayLayout.Override.ActiveRowAppearance.Reset();
            lblLuuY.Text = "Đơn vị có trách nhiệm nộp toàn bộ số tiền bảo hiểm phải đóng"
                + " (hoặc số tiền bảo hiểm một tháng tham gia của toàn bộ người lao động trong \ndanh sách) "
                + " trong thời gian 03 ngày làm việc kể từ ngày cơ quan BHXH nhận hồ sơ để được cấp thẻ BHYT.";
            this.dateTimePicker1.CustomFormat = "dd/MM/yyyy";
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[0].Width = 10;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[1].Width = 140;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[2].Width = 20;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[3].Width = 20;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[0].CellActivation = Activation.NoEdit;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns[1].CellActivation = Activation.NoEdit;

            this.uG_DanhMucHS.Rows[1].Cells[2].Activation = Activation.NoEdit;
            this.uG_DanhMucHS.Rows[2].Cells[2].Activation = Activation.NoEdit;
            this.uG_DanhMucHS.Rows[5].Cells[2].Activation = Activation.NoEdit;
            this.uG_DanhMucHS.DisplayLayout.Bands[0].Columns["LoaiHienThi"].Hidden = true;

            for (int i = 0; i < uG_DanhMucHS.Rows.Count; i++)
            {
                if (this.uG_DanhMucHS.Rows[i].Cells[4].Value.ToString() == "0")
                {
                    uG_DanhMucHS.Rows[i].Cells[2].Hidden = true;
                    uG_DanhMucHS.Rows[i].Cells[3].Hidden = true;                    
                }                    
            }


            for (int i = 0; i < uG_DanhMucHS.Rows.Count; i++)
            {
                if (this.uG_DanhMucHS.Rows[i].Cells[2].Value.ToString() == "False")
                {
                    this.uG_DanhMucHS.Rows[i].Cells[3].Hidden = true;
                }
            }
            //row 4
            if (this.uG_DanhMucHS.Rows[4].Cells[2].Value.ToString() == "False")
            {
                this.ultraTabPageControl3.Tab.Visible = false;
            }
            else
            {
                this.ultraTabPageControl3.Tab.Visible = true;
            }
            //row 6
            if (this.uG_DanhMucHS.Rows[6].Cells[2].Value.ToString() == "False")
            {
                this.ultraTabPageControl6.Tab.Visible = false;
            }
            else
            {
                this.ultraTabPageControl6.Tab.Visible = true;
            }
            //row 7
            if (this.uG_DanhMucHS.Rows[7].Cells[2].Value.ToString() == "False")
            {
                this.ultraTabPageControl5.Tab.Visible = false;
            }
            else
            {
                this.ultraTabPageControl5.Tab.Visible = true;
            }
            //row 11
            if (this.uG_DanhMucHS.Rows[11].Cells[2].Value.ToString() == "False")
            {
                this.ultraTabPageControl7.Tab.Visible = false;
            }
            else
            {
                this.ultraTabPageControl7.Tab.Visible = true;
            }
            //row 12
            if (this.uG_DanhMucHS.Rows[12].Cells[2].Value.ToString() == "False")
            {
                this.ultraTabPageControl4.Tab.Visible = false;
            }
            else
            {
                this.ultraTabPageControl4.Tab.Visible = true;
            }
        }

        public DataTable GetAllHoSo()
        {
            IList<hoso> a = _hosoSrv.layTatCaHoSo_DKBHXH_101();
            DataTable table = new DataTable();
            table.Columns.Add("STT", typeof(string));
            table.Columns.Add("Loại giấy tờ, biểu mẫu", typeof(string));
            table.Columns.Add("Chọn", typeof(bool));

            table.Columns.Add("Số lượng", typeof(int));
            table.Columns.Add("LoaiHienThi", typeof(string));
            
            foreach (var item in a)
            {
                table.Rows.Add(item.STT, item.Ten, item.TrangThaiChon, item.SoLuong,item.LoaiHienThi);                
            }

            return table;
        }

        private void uG_DanhMucHS_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            UltraGridColumn ugc = uG_DanhMucHS.DisplayLayout.Bands[0].Columns[2];
            ugc.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
            ugc.CellActivation = Activation.AllowEdit;
        }

        private void uG_DanhMucHS_CellChange(object sender, CellEventArgs e)
        {
            bool checkedState = false;
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Column.Index, 2))
            {
                checkedState = bool.Parse(e.Cell.Text);
            }
            //Hide show row 4
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, 4))
            {
                if (checkedState == true)
                {
                    this.ultraTabPageControl3.Tab.Visible = true;
                }
                else if (checkedState == false)
                {

                    this.ultraTabPageControl3.Tab.Visible = false;

                }
            }
            //Hide show row 6
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, 6))
            {
                if (checkedState == true)
                {
                    this.ultraTabPageControl6.Tab.Visible = true;
                }
                else if (checkedState == false)
                {

                    this.ultraTabPageControl6.Tab.Visible = false;
                }
            }
            //Hide show row 7
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, 7))
            {
                if (checkedState == true)
                {
                    this.ultraTabPageControl5.Tab.Visible = true;
                }
                else if (checkedState == false)
                {

                    this.ultraTabPageControl5.Tab.Visible = false;
                }
            }
            //Hide show row 11
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, 11))
            {
                if (checkedState == true)
                {
                    this.ultraTabPageControl7.Tab.Visible = true;
                }
                else if (checkedState == false)
                {

                    this.ultraTabPageControl7.Tab.Visible = false;
                }
            }
            //Hide show row 12
            if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, 12))
            {
                if (checkedState == true)
                {
                    this.ultraTabPageControl4.Tab.Visible = true;
                }
                else if (checkedState == false)
                {

                    this.ultraTabPageControl4.Tab.Visible = false;
                }
            }

            //Hide show soluong
            for (int i = 0; i < uG_DanhMucHS.Rows.Count; i++)
            {
                if (StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Row.Index, i))
                {
                    if (checkedState == true)
                    {
                        uG_DanhMucHS.Rows[e.Cell.Row.Index].Cells[3].Hidden = false;
                    }
                    else if (checkedState == false)
                    {
                        uG_DanhMucHS.Rows[e.Cell.Row.Index].Cells[3].Hidden = true;
                    }
                }
            }
        }
        #endregion

        #region UG_A01aTS
        public void LoadComboBox()
        {
            uc_Quoctich = new UltraCombo();
            uc_TinhThanhPho = new UltraCombo();
            uc_Loai = new UltraCombo();
            uc_Dantoc = new UltraCombo();
            uc_Benhvien = new UltraCombo();

            uc_Quoctich.BindingContext = this.BindingContext;
            uc_TinhThanhPho.BindingContext = this.BindingContext;
            uc_Loai.BindingContext = this.BindingContext;
            uc_Dantoc.BindingContext = this.BindingContext;
            uc_Benhvien.BindingContext = this.BindingContext;

            uc_Quoctich.DataSource = EnumHelper.GetDescriptionForBind(QuocTich.QT1).ToList();
            uc_Quoctich.DisplayMember = "ValueString";
            uc_Quoctich.ValueMember = "ValueInt";

            var ListThanhPho = _thanhphoSrv.layTatCaThanhPho();
            uc_TinhThanhPho.DataSource = ListThanhPho;
            uc_TinhThanhPho.DisplayMember = "Ten";
            uc_TinhThanhPho.ValueMember = "ID";

            uc_Loai.DataSource = EnumHelper.GetDescriptionForBind(BHXH_Loai_DSLD.Loai1).ToList();
            uc_Loai.DisplayMember = "ValueString";
            uc_Loai.ValueMember = "ValueInt";

            uc_Dantoc.DataSource = EnumHelper.GetDescriptionForBind(DanToc.DT1).ToList();
            uc_Dantoc.DisplayMember = "ValueString";
            uc_Dantoc.ValueMember = "ValueInt";

            var ListBenhVien = _benhvienSrv.layTatCaBenhVien();
            uc_Benhvien.DataSource = ListBenhVien;
            uc_Benhvien.DisplayMember = "Ten";
            uc_Benhvien.ValueMember = "ID";
        }

        public void LoadGridview_A01aTS()
        {
            this.uG_A01aTS.DisplayLayout.Bands[0].RowLayoutStyle = RowLayoutStyle.ColumnLayout;
            this.uG_A01aTS.DisplayLayout.Bands[0].Override.AllowRowLayoutLabelSizing = RowLayoutSizing.Both;
            this.uG_A01aTS.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
            this.uG_A01aTS.SyncWithCurrencyManager = false;
            //this.uG_A01aTS.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.False;
        }

        public DataTable GetAll_A01aTS_Tab()
        {
            IList<hs_A01aTS_dt> a = _hs_01adtSrv.layTatCahs_01adt();
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("STT \n(1)", typeof(string));
            table.Columns.Add("Họ và tên \n(2)", typeof(string));
            table.Columns.Add("Quốc tịch \n(3)", typeof(int));
            table.Columns.Add("Số CMND \n(4)", typeof(string));
            table.Columns.Add("Ngày cấp \n(5)", typeof(string));
            table.Columns.Add("Mã tỉnh cấp \n(6)", typeof(int));
            table.Columns.Add("Loại \n(7)", typeof(int));
            table.Columns.Add("Chưa có số BHXH \n(8)", typeof(bool));
            table.Columns.Add("Mã số BHXH \n(9)", typeof(string));
            table.Columns.Add("Có nhiều hơn 1 mã số BHXH \n(10)", typeof(bool));
            table.Columns.Add("Ngày, tháng, năm sinh \n(11)", typeof(string));
            table.Columns.Add("Nữ(X) \n(12)", typeof(bool));
            table.Columns.Add("Dân tộc \n(13)", typeof(int));
            table.Columns.Add("Hộ khẩu thường trú \n(14)", typeof(string));
            table.Columns.Add("Nơi đăng ký KCB ban đầu (Tỉnh) \n(15)", typeof(int));
            table.Columns.Add("Nơi đăng ký KCB ban đầu (Bệnh viện) \n(16)", typeof(int));
            table.Columns.Add("Quyền lợi hưởng BHXH \n(17)", typeof(string));
            table.Columns.Add("Mã phòng ban \n(18)", typeof(string));
            table.Columns.Add("Ghi chú \n(19)", typeof(string));

            foreach (var item in a)
            {

                table.Rows.Add(item.ID, item.CT01, item.CT02, item.QuocTich, item.CT04, item.CT05, item.CT06, item.Loai
                    , item.CT08, item.CT09, item.CT10, item.CT11, item.CT12, item.CT13, item.CT14, item.CT15, item.CT16, item.NoBHXH, item.MaPB, item.MoreBHXH);
            }

            return table;
        }

        private void uG_A01aTS_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {

            e.Layout.Bands[0].Columns[3].EditorComponent = uc_Quoctich;
            e.Layout.Bands[0].Columns[6].EditorComponent = uc_TinhThanhPho;
            e.Layout.Bands[0].Columns[7].EditorComponent = uc_Loai;
            e.Layout.Bands[0].Columns[13].EditorComponent = uc_Dantoc;
            e.Layout.Bands[0].Columns[15].EditorComponent = uc_TinhThanhPho;
            e.Layout.Bands[0].Columns[16].EditorComponent = uc_Benhvien;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                e.Layout.Bands[0].Columns[i].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
            }

        }

        private void uG_A01aTS_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Insert:
                    this.uG_A01aTS.DisplayLayout.Bands[0].AddNew();
                    break;
                case Keys.F11:
                    if (this.uG_A01aTS.Selected.Rows.Count > 0)
                    {
                        ID_A01aTS.Add(this.uG_A01aTS.ActiveRow.Cells[0].Value.ToString());
                        this.uG_A01aTS.DeleteSelectedRows();
                    }
                    else
                    {
                        MessageBox.Show("Chọn dòng để xóa.");
                    }

                    break;
            }
        }
        #endregion

        #region Thao_Tac_Cac_Nut

        public void Ghi()
        {
            string ID = "";
            bool update_hs_01adt = false;
            bool add_hs_01adt = false;
            hs_A01aTS_dt hs = new hs_A01aTS_dt();
            UltraGridBand band = this.uG_A01aTS.DisplayLayout.Bands[0];
            foreach (UltraGridRow row in band.GetRowEnumerator(GridRowType.DataRow))
            {
                UltraGridColumn column = this.uG_A01aTS.DisplayLayout.Bands[0].Columns[0];
                ID = row.Cells[column].Value.ToString();
                hs.CT01 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[1]].Value.ToString();
                hs.CT02 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[2]].Value.ToString();
                hs.QuocTich = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[3]].Value.ToString();
                hs.CT04 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[4]].Value.ToString();
                hs.CT05 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[5]].Value.ToString();
                hs.CT06 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[6]].Value.ToString();
                hs.Loai = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[7]].Value.ToString();
                hs.CT08 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[8]].Value.ToString();
                hs.CT09 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[9]].Value.ToString();
                hs.CT10 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[10]].Value.ToString();
                hs.CT11 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[11]].Value.ToString();
                hs.CT12 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[12]].Value.ToString();
                hs.CT13 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[13]].Value.ToString();
                hs.CT14 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[14]].Value.ToString();
                hs.CT15 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[15]].Value.ToString();
                hs.CT16 = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[16]].Value.ToString();
                hs.NoBHXH = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[17]].Value.ToString();
                hs.MaPB = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[18]].Value.ToString();
                hs.MoreBHXH = row.Cells[this.uG_A01aTS.DisplayLayout.Bands[0].Columns[19]].Value.ToString();
                if (!ID.Equals(""))
                {
                    hs.ID = int.Parse(ID);
                    if (_hs_01adtSrv.Update_hs_01adt(hs) > 0)
                    {
                        update_hs_01adt = true;
                    }
                }
                else
                {
                    if (_hs_01adtSrv.Add_hs_01adt(hs) == true)
                    {
                        add_hs_01adt = true;
                    }
                }
            }

            if (update_hs_01adt == true || add_hs_01adt == true)
            {
                MessageBox.Show("Cập nhật thông tin thành công...");
            }
        }

        public void Xoa()
        {
            bool Xoa_hs_01adt = false;
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                foreach (var item in ID_A01aTS)
                {
                    if (_hs_01adtSrv.Delete_hs_01adt(item) > 0)
                    {
                        Xoa_hs_01adt = true;
                    }
                }
                if (Xoa_hs_01adt == true)
                {
                    MessageBox.Show("Xóa thông tin thành công...");
                }

            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            Ghi();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Exit_App();
        }

        public void Exit_App()
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Application.ExitThread();
            }
        }

        private void ultraTabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F3:
                    Xoa();
                    break;
                case Keys.F5:
                    Ghi();
                    break;
                case Keys.F12:
                    Exit_App();
                    break;
            }
        }

        #endregion




    }
}
